<?php
$this_file = str_replace( '\\', '/', __FILE__ );
$this_file = explode( 'wp-content', $this_file );
$this_file = $this_file[ 0 ];
require( $this_file . 'wp-blog-header.php' );
header( 'HTTP/1.1 200 OK' );

// theme options
$aOptions      = SofaSuppaStore::initOptions( false );
$currency_code = $aOptions[ 'paypalcurrency' ];
$checkout_tax  = floatval( $aOptions[ 'checkout_tax' ] );
$paypal_mode   = $aOptions[ 'paypal_mode' ];
$ppal          = $aOptions[ 'ppal' ];
$checkout_voucher = stripslashes( strip_tags( $aOptions[ 'checkout_voucher' ] ) );
$checkout_ship    = stripslashes( strip_tags( $aOptions[ 'checkout_ship' ] ) );

// other PayPal stuff
$return_fromPP        = home_url() . '/?dc=pp'; // return from PayPal
$cancel_return_fromPP = get_permalink( intval( get_option( 'checkout' ) ) ); // cancel from Paypal
$ping_ipn             = get_template_directory_uri() . '/gateways/paypal_ipn.php'; // PayPal notification page

$custom_image_url     = get_template_directory_uri() . '/images/logo_paypal.png'; // paypal store logo
$back_button          = __( "Back to", "sofa_suppastore" ) . ' ' . get_bloginfo( 'name' ); // back button label

// any discount?
$percent_discount = 0;
$discount = !empty( $_POST[ 'meta_cc' ] ) ? floatval( $_POST[ 'meta_cc' ] ) : 0;
if( $discount > 0 ) {
	$percent_discount = number_format( $discount, 2, '.', '' );
	if( $percent_discount >= 100 ) $percent_discount = 99.99;
}
// handling?
$handling_price = 0;
$handling = !empty( $_POST[ 'meta_hc' ] ) ? floatval( $_POST[ 'meta_hc' ] ) : 0;
if( $handling > 0 ) $handling_price = number_format( $handling, 2, '.', '' );

// Include the paypal library
locate_template( array( 'gateways/Paypal.php' ), true, true );

// Create an instance of the paypal library
$myPaypal = new Paypal();
$myPaypal->addField( 'lc', $currency_code );
$myPaypal->addField( 'business', $ppal );
$myPaypal->addField( 'currency_code', $currency_code );
$myPaypal->addField( 'return', $return_fromPP );
$myPaypal->addField( 'cancel_return', $cancel_return_fromPP );
$myPaypal->addField( 'notify_url', $ping_ipn );
$myPaypal->addField( 'image_url', $custom_image_url );
$myPaypal->addField( 'cbt', $back_button );
if( $handling_price > 0 ) $myPaypal->addField( 'handling_cart', $handling_price );

// what's in da basket?
if( isset( $_COOKIE[ 'sofasuppastore' ] ) ) {
	
	$ppal_custom = '';
	$ppal_count  = 1;
	$items_sum   = 0;
	
	foreach( $_COOKIE[ 'sofasuppastore' ] as $name => $value ) {
		
		$single_item_arr   = explode( '~', $value ); // 0 - name (ID), 1 - size, 2 - quantity, 3 - price, 4 - selected row, 5 - stock, 6 - shipp
		$ppal_custom .= ( $single_item_arr[ 0 ] . '~' . $single_item_arr[ 1 ] . '~' . $single_item_arr[ 2 ] . '~' . $single_item_arr[ 3 ] . '~' . $single_item_arr[ 5 ] . '~' . $single_item_arr[ 6 ] . '^' );
		
		$single_item_name  = get_the_title( intval( $single_item_arr[ 0 ] ) ); // make prod title shorter
		$single_item_id    = intval( $single_item_arr[ 0 ] ); // product id
		$single_item_size  = $single_item_arr[ 1 ]; // product size
		$single_item_qty   = intval( $single_item_arr[ 2 ] ); // quantity
		$single_item_price = number_format( $single_item_arr[ 3 ], 2, '.', '' ); // single price
		$single_item_shipp = ( $single_item_arr[ 6 ] ) ? ( $single_item_arr[ 6 ] * $single_item_qty ) : 0; // shipping price
		
		// populate PP variables
		$myPaypal->addField( 'item_name_' . $ppal_count, $single_item_name ); // product name
		$myPaypal->addField( 'item_number_' . $ppal_count, $single_item_id ); // product id
		$myPaypal->addField( 'quantity_' . $ppal_count, $single_item_qty ); // quantity
		$myPaypal->addField( 'on0_' . $ppal_count, __( "Size", "sofa_suppastore" ) ); // option name: size
		$myPaypal->addField( 'os0_' . $ppal_count, $single_item_size ); // product size
		$myPaypal->addField( 'amount_' . $ppal_count, $single_item_price );  // single price
		if( $checkout_tax > 0 ) $myPaypal->addField( 'tax_rate_' . $ppal_count, $checkout_tax );
		if( $single_item_shipp > 0 ) $myPaypal->addField( 'shipping_' . $ppal_count, number_format( $single_item_shipp, 2, '.', '' ) );  // shipping price
		if( $percent_discount > 0 ) $myPaypal->addField( 'discount_rate_' . $ppal_count, $percent_discount );  // discount related to coupon code percentage
		
		$ppal_count ++;
		
	}
	
	// pack it up in custom for return
	$myPaypal->addField( 'custom', $ppal_custom );
}

// Test mode?
if( $paypal_mode == 'sandbox' ) $myPaypal->enableTestMode();

// Let's start the train!
$myPaypal->submitPayment();